package com.twu.biblioteca.service;

import com.twu.biblioteca.Library;
import com.twu.biblioteca.exception.InvalidCheckReturnException;
import com.twu.biblioteca.model.Movie;

import java.util.List;

import static com.twu.biblioteca.model.Status.available;
import static com.twu.biblioteca.model.Status.lended;
import static com.twu.biblioteca.util.Messages.*;

public class MovieService {
    private Library library;
    private UserService userService;

    public void setLibrary(Library library) {
        this.library = library;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    public Movie checkoutMovie(int movieId, String accountId) throws InvalidCheckReturnException {
        boolean flag = userService.checkLogin(accountId);
        if (flag) {
            Movie movie = library.findMovie(movieId);
            if (movie != null && movie.getStatus().equals(available)) {
                library.markMovieStatus(movie, lended);
                return movie;
            } else {
                throw new InvalidCheckReturnException(THAT_MOVIE_IS_NOT_AVAILABLE);
            }
        } else {
            throw new InvalidCheckReturnException(PLEASE_LOGIN_FIRST_YOU_CAN_INPUT_COMMAND_LOGIN_TO_LOGIN_THE_SYSTEM);
        }
    }

    public Movie returnMovie(int movieId, String accountId) throws InvalidCheckReturnException {
        boolean flag = userService.checkLogin(accountId);
        if (flag) {
            Movie movie = library.findMovie(movieId);
            if (movie != null && movie.getStatus().equals(lended)) {
                library.markMovieStatus(movie, available);
                return movie;
            } else {
                throw new InvalidCheckReturnException(THAT_IS_NOT_A_VALID_MOVIE_TO_RETURN);
            }
        } else {
            throw new InvalidCheckReturnException(PLEASE_LOGIN_FIRST_YOU_CAN_INPUT_COMMAND_LOGIN_TO_LOGIN_THE_SYSTEM);
        }
    }

    public List<Movie> getAllMovies() {

        return library.getMovies();
    }
}
